﻿using Avalon.Core.Providers;
using System.Web.Mvc;

namespace Avalon.Core.MVC {
    public class AvalonViewFactory : RazorViewEngine {
        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath) {
            if (ContextProvider.RendererType == RendererType.DefaultRenderer) {
                return base.CreateView(controllerContext, viewPath, masterPath);
            }
            else if (ContextProvider.RendererType == RendererType.ViewRenderer) {
                return new AvalonRazorView(controllerContext, ContextProvider.ControllerDescriptor.RendererDescriptor);
            }
            else {
                return new AvalonCompiledView(controllerContext, viewPath);
            }
        }

        protected override bool FileExists(ControllerContext controllerContext, string virtualPath) {
            if (ContextProvider.RendererType == RendererType.DefaultRenderer)
                return base.FileExists(controllerContext, virtualPath);
            else
                return true;
        }


        public IView CreateAvalonView(ControllerContext controllerContext, string viewPath, string masterPath) {
            return this.CreateView(controllerContext, viewPath, masterPath);
        }
    }
}